<?php

class Sistema_QuadrohorariosController extends App_Controller_Action
{
    
    private $_table     = 'QUADRO_HORARIOS';
    private $_pk        = 'QUH_ID';
    private $_order_col = 'QUH_NOM';
    private $_order     = 'ASC';
    private $_sta_col   = 'QUH_STA';
    private $obj        = '';
    private $form       = '';
    public  $dir        = 'public/upload//';
    public  $_file_col  = 'QUH_FILE';
    
    public function init()
    {
        //if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Professor();
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('elemform','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_list.js');
        
        $where = $this->_pk.' > 0';
        
        $stafiltro = $this->_getParam('stafiltro', '1'); 
        if($stafiltro != '') $where .= ' AND QUH_STA = ' . $stafiltro;
        $this->view->stafiltro  = $stafiltro;
        
        $itfiltro = $this->_getParam('itfiltro', ''); 
        if($itfiltro != '') $where .= ' AND QUH_NOM like "' . $itfiltro.'%"';
        $this->view->itfiltro = $itfiltro; 
        
        $query = 'SELECT SQL_CACHE q.*, cb.CBS_NOM, n.NRT_NOM FROM '.$this->_table.' q, CURSO c, CURSOBASE cb, NUMEROTURMA n  WHERE q.CUR_ID = c.CUR_ID AND q.CBS_ID = cb.CBS_ID AND c.CBS_ID = cb.CBS_ID AND c.CUR_TRM = n.NRT_ID AND '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order.'';
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
        
    }
    
    
    public function editAction() {
        
        Zend_Loader::loadClass('datetools','library');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/chosen/chosen.min.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/chosen/chosen.jquery.min.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_quadrohorarios.js?1=8'); 
        
       
        //$this->view->form = $this->form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if(1){ //$this->form->isValid($formData)) {
                    $id = (int) $formData[$this->_pk]; //$this->form->getValue($this->_pk);

                    $data = $formData;//$this->form->getValues();
                    //print_r($data);
                    unset($data['submit']);
                    unset($data['PRS_ID']);
                    
                    $data['QUH_STA'] = isset($data['QUH_STA']) ? $data['QUH_STA']:'0';
                    
                    if ($id == 0){
                        $this->db->insert($this->_table,$data);
                    }else{                      
                        $this->db->update($this->_table,$data, $this->_pk.' = ' . $id);
                    }
                    
                    $this->db->delete('QUADRO_HORARIOS_PRS',$this->_pk .' = ' . $id);
                    foreach($formData['PRS_ID'] as $sem => $val){
                        
                        foreach($val as $num => $prs){
                            //echo $sem.' - '.$num.' - '.$prs.'<br>';
                            if($prs > 0){
                                $arr = array(
                                    'QUH_ID'    => $id,
                                    'PRS_ID'    => $prs,
                                    'QHP_SEM'   => $sem,
                                    'QHP_NUM'   => $num,
                                );

                                $this->db->insert('QUADRO_HORARIOS_PRS',$arr);
                            }
                        }
                    }
                    
                    $this->_helper->redirector('index');      
                } else {
                    $this->view->data = $formData;//$this->form->populate($formData);
                }
            } catch(Exception $ex) {
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->view->data = $formData;//$this->form->populate($formData);
            }  
        } else {
            $id = $this->_getParam('id', 0);
            $prs = array();
            if ($id > 0) {
                $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk .' = ' . $id;
                $data = $this->db->fetchRow($query);
                
                $this->view->data = $data;//$this->form->populate($data);   
                
                $query = 'SELECT SQL_CACHE QHP_SEM,QHP_NUM,PRS_ID FROM QUADRO_HORARIOS_PRS WHERE '.$this->_pk .' = ' . $id;
                $rows = $this->db->fetchAll($query);
                foreach($rows as $row) {
                    $prs[$row['QHP_SEM']][$row['QHP_NUM']] = $row['PRS_ID'];
                }
            }
            
            $query = 'SELECT SQL_CACHE CBS_ID,CBS_NOM FROM CURSOBASE WHERE CBS_STA = 1 ORDER BY CBS_ORD ASC';
            $data = $this->db->fetchAll($query);
            $this->view->cursos = $data;
            
            $this->view->prs = $prs;
        }
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $data = array($this->_sta_col => 0);
            $ok = (bool) $this->db->update($this->_table,$data, $this->_pk .' = ' . $id);
            
        }
        
        echo json_encode(array('ok' => $ok));
    }
    
    public function turmasAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $html = '';
        $ok = TRUE;
        
        
        $id = $this->_getParam('id', 0);
        //if ((int)$id > 0 ) {
            
            $query = 'SELECT SQL_CACHE c.CUR_ID, cb.CBS_NOM, cb.CBS_TPOTMP, n.NRT_NOM FROM CURSO c, CURSOBASE cb, NUMEROTURMA n WHERE c.CBS_ID = '.$id.' AND c.CBS_ID = cb.CBS_ID AND c.CUR_TRM = n.NRT_ID ORDER BY n.NRT_SEM ASC, n.NRT_NOM ASC, c.CUR_NOM ASC';
            $row = $this->db->fetchAll($query);
            
            $html .= '<select id="CUR_ID" name="CUR_ID" required=""><option value="">Selecione uma Turma</option>';
            foreach($row as $r){ 
                $html .= '<option value="'.$r['CUR_ID'].'">'.$r['NRT_NOM'].'</option>';
            } 
            $html .= '</select>';
            
        //}
        
        echo json_encode(array('ok' => $ok,'html'=>$html));
    }
    
    public function professoresAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $html = '';
        $ok = TRUE;
        
        
        $cbs_id = $this->_getParam('cbs_id', 0);
        $cur_id = $this->_getParam('cur_id', 0);
        
        if ((int) $cbs_id > 0 ) {
            
            $query = 'SELECT SQL_CACHE PRS_ID, PRS_NOM, DCP_NOM FROM PROFESSOR p, DISCIPLINA d '
            . 'WHERE p.DCP_ID = d.DCP_ID AND PRS_STA = 1 '
            . 'AND CBS_ID like "%'.$cbs_id.',%" AND CUR_ID like "%'.$cur_id.',%" ORDER BY PRS_NOM';
            $row = $this->db->fetchAll($query);
            
            //$html = '<select id="PRS_ID[]" name="PRS_ID[]" class="chosen-select">';
            $html .= '<option value="">Professor</option>';
            foreach($row as $r){           
                $html .= '<option '.$selected.' value="'.$r['PRS_ID'].'">'.$r['PRS_NOM'].' '.$r['DCP_NOM'].'</option>'; 
            }
            //$html .= '</select>';
            
        }
        
        echo json_encode(array('ok' => $ok,'html'=>$html));
    }
    
    
    
}

